#!/bin/sh
set -e

PATH=/sbin:/usr/sbin:/bin:/usr/bin

FSCK=/bin/fsck_mfs
ACPI=/service/acpi

if [ X`sysenv arch` = Xi386 ]
then	if [ -e $ACPI -a -n "`sysenv acpi`" ]
	then
		minix-service -c up $ACPI
	fi
	minix-service -c up /service/pci -dev /dev/pci

	minix-service -c up /service/input -dev /dev/kbdmux
	minix-service -c up /service/pckbd || :

	# Start procfs so we can access /proc/pci
	mount -t procfs none /proc >/dev/null

	# Do we want to use the virtio block device?
	# If not specified, default to yes if the device is found.
	if sysenv virtio_blk >/dev/null
	then	virtio_blk="`sysenv virtio_blk`"
        elif	grep '^[^ ]* [^ ]* 1AF4:1001[^ ]* ' /proc/pci >/dev/null
	then	echo "virtio_blk not set, defaulting to using found virtio device."
		virtio_blk=yes
        fi

	minix-service -cn up /service/floppy -dev /dev/fd0
	if [ X`sysenv ahci` = Xyes ]
	then
		# this is here temporarily, for testing purposes
		minix-service -c up /service/ahci -dev /dev/c0d0 -label ahci_0 -args instance=0
	elif [ X"$virtio_blk" = Xyes ]
	then
		minix-service -c up /service/virtio_blk -dev /dev/c0d0 -label virtio_blk_0 -args instance=0
	else
		minix-service -c up /service/at_wini -dev /dev/c0d0 -label at_wini_0
		minix-service -cr up /service/at_wini -dev /dev/c1d0 -label at_wini_1 -args instance=1 2>/dev/null || :
	fi
	umount /proc >/dev/null
fi

if [ X`sysenv arch` = Xearm ]
then	echo Starting the mmc driver
	minix-service -c up /service/mmc -dev /dev/c0d0
fi

# Load ProcFS from the ramdisk to minimize the chance of a desync with the boot
# image services from which it obtains data structures directly.  As we move to
# the MIB service, this will eventually become obsolete.
minix-service up /service/procfs || echo "WARNING: couldn't start procfs"

if sysenv rootdevname >/dev/null
then	rootdevname=/dev/`sysenv rootdevname`
else
	if ! sysenv cdproberoot >/dev/null && ! sysenv bootramdisk >/dev/null
	then	echo "rootdevname not set"
		exit 1
	fi
fi

if [ "`sysenv bin_img`" = 1 ]
then
    bin_img="-i "
fi

if sysenv cdproberoot >/dev/null
then
	echo
	echo 'Looking for boot CD. This may take a minute.'
	echo 'Please ignore any error messages.'
	echo
	rootdevname=$(cdprobe) || { echo 'No CD found'; exit 1; }
	export rootdevname
elif [ "$rootdevname" = "/dev/ram" ]
then
	ramimagename=/dev/`sysenv ramimagename`
	echo "Loading ramdisk from $ramimagename"
	loadramdisk "$ramimagename" || echo "WARNING: loadramdisk failed"
fi

if sysenv bootramdisk >/dev/null
then
	rootdevname=imgrd
fi

echo "Root device name is $rootdevname"

if ! sysenv cdproberoot >/dev/null
then
	if [ -e $FSCK ]
	then	$FSCK -p $rootdevname
	fi
fi

# Change root from temporary boot ramdisk to the configure root device
if ! sysenv bootramdisk >/dev/null; then
	mount -n $bin_img"$rootdevname" /

	# Reopen standard file descriptors, so that we can unmount the ramdisk.
	# That is essentially a VFS shortcoming that should be fixed, though..
	exec >/dev/log
	exec 2>/dev/log
	exec </dev/console
fi

# Mount the ProcFS instance that was loaded from the ramdisk, on the root FS.
mount -e -n -t procfs none /proc || echo "WARNING: couldn't mount procfs"

# Start the NetBSD rc infrastructure
if ! sysenv bootramdisk >/dev/null; then
	exec sh /etc/rc "$@"
fi
